﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZhiAo.CommonLibrary;
using System.Data;
using ZhiAo.WebLib.Data;
using ZhiAo.Management;
using ZhiAo.Management.Data;
using System.Text.RegularExpressions;

public partial class ShuiWenJu_RepRealTime_GeneralRpt : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState["orderdire"] = "asc";
            DdlDataBind.BindCityData(ddlCity);
            LoadData();
        }
    }

    private string Validate()
    {        
        string stationName=this.txtStation.Text;
        //string rainValue=this.txtRainValue.Text;
        string msg = string.Empty;
        string regexStr = @"\W+";
        Regex r = new Regex(regexStr);
        if (!string.IsNullOrEmpty(stationName) && r.IsMatch(stationName))
        {
            msg += "站名请输入汉字;";
        }
        //regexStr = @"^\d+$|^\d+\.{1}\d+$";
        //r = new Regex(regexStr);
        //if ((!string.IsNullOrEmpty(rainValue)) && (!r.IsMatch(rainValue)))
        //{
        //    msg += "雨量值请输入整数或小数;";
        //}
        return msg;
    }

    private void LoadData()
    {
        string userId = UserInfo.CurrentUserInfo.User.ID;
        string condition = this.GetQueryWhere();
        DataTable dt = GeneralData.GetData(userId, condition);
        DataView dv = dt.DefaultView;
        if (ViewState["sortorder"] != null)
        {
            dv.Sort = (string)ViewState["sortorder"] + " " + (string)ViewState["orderdire"];
        }
        gvData.DataSource = dv;
        gvData.DataBind();
    }


    private string GetQueryWhere()
    {
        string shi = this.ddlCity.SelectedValue;
        string xian = this.ddlXian.SelectedValue;
        string stationType = this.ddlStationType.SelectedValue;
        string stationName = this.txtStation.Text;
        //string rainType = this.ddlRain.SelectedValue;
        //string rainValue = this.txtRainValue.Text;
        string condition = string.Empty;
        if (!string.IsNullOrEmpty(shi))
        {
            condition += " and g.shi='" + shi + "' ";
        }
        if (!string.IsNullOrEmpty(xian))
        {
            condition += " and g.xian='" + xian + "' ";
        }
        if (!string.IsNullOrEmpty(stationType))
        {
            condition += " and g.sttp='" + stationType + "' ";
        }
        if (!string.IsNullOrEmpty(stationName))
        {
            condition += " and g.stnm like '%" + stationName + "%' ";
        }
        //if (!string.IsNullOrEmpty(rainValue))
        //{
        //    condition += " and g." + rainType + " >= " + rainValue;
        //}
        return condition;
    }


    //导出
    protected void btnExport_Click(object sender, EventArgs e)
    {
        string msg = Validate();
        if (!string.IsNullOrEmpty(msg))
        {
            this.lblMsg.Visible = true;
            this.lblMsg.Text = msg;
            return;
        }
        ExcelHelper excel = new ExcelHelper();
        string userId = UserInfo.CurrentUserInfo.User.ID;
        string condition = this.GetQueryWhere();
        DataTable dtData = GeneralData.GetData(userId, condition);
        if (dtData.Rows.Count > 0)
        {
            //excel.Export(dtData);
            excel.Export(gvData, dtData);
        }
    }

    //查询
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string msg = Validate();
        if (!string.IsNullOrEmpty(msg))
        {
            this.lblMsg.Visible = true;
            this.lblMsg.Text = msg;
            return;
        }
        gvData.PageIndex = 0;
        LoadData();        
    }
    protected void gvData_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        string msg = Validate();
        if (!string.IsNullOrEmpty(msg))
        {
            this.lblMsg.Visible = true;
            this.lblMsg.Text = msg;
            return;
        }
        this.gvData.PageIndex = e.NewPageIndex;
        LoadData();
    }

    /// <summary>
    /// 市下拉框改变
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
    {
        string city = ddlCity.SelectedValue;
        DdlDataBind.BindXianData(ddlXian, city);
    }


    
    /// <summary>
    /// gridview排序
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gvData_Sorting(object sender, GridViewSortEventArgs e)
    {
        ViewState["sortorder"] = e.SortExpression;
        if (ViewState["orderdire"].ToString() == "desc")
        {
            ViewState["orderdire"] = "asc";
        }
        else
        {
            ViewState["orderdire"] = "desc";
        }
        LoadData();
    }
}
